package com.happyhell.hiyou.rest.controllers;

import java.util.Collection;

import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.happyhell.hiyou.client.UserInfoMapper;
import com.happyhell.hiyou.model.UserInfo;
import com.happyhell.hiyou.model.UserInfoExample;
import com.happyhell.hiyou.rest.interceptors.UserAware;
import com.happyhell.hiyou.util.SqlSessionUtil;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.ValidationAwareSupport;


@Results({
    @Result(name="success", type="redirectAction", params = {"actionName" , "user-infos"})
})
public class UserInfosController extends ValidationAwareSupport implements ModelDriven<Object>, Validateable, UserAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private UserInfo model = new UserInfo();
    private String id;
    private Collection<UserInfo> list;
    private SqlSession sqlSession = SqlSessionUtil.getSession();
    private UserInfoMapper userMapper = sqlSession.getMapper(UserInfoMapper.class);
    private UserInfo user;
    
    // GET /userInfo/1
    public HttpHeaders show() {
        return new DefaultHttpHeaders("show");
    }
    
    // GET /userInfos
    public HttpHeaders index() {
    	String condition = model.getCondition();
    	UserInfoExample example = new UserInfoExample();
    	if (condition != null && !"".equals(condition)) {    		
    		example.or().andUiIdLessThan(Integer.valueOf(condition));
    	}
        list = userMapper.selectByExample(example);
        return new DefaultHttpHeaders("index")
            .disableCaching();
    }
    
    // GET /userInfos/1/edit
    public String edit() {
        return "edit";
    }

    // GET /userInfos/new
    public String editNew() {
        model = new UserInfo();
        return "editNew";
    }
    
    // GET /userInfos/1/deleteConfirm
    public String deleteConfirm() {
        return "deleteConfirm";
    }

    // DELETE /userInfos/1
    public String destroy() {
    	userMapper.deleteByPrimaryKey(Integer.valueOf(id));
        addActionMessage("User removed successfully");
        return "success";
    }
    
    // POST /orders
    public HttpHeaders create() {
    	userMapper.insert(model);
        addActionMessage("New user created successfully");
        return new DefaultHttpHeaders("success")
            .setLocationId(model.getUiId());
    }

    // PUT /orders/1
    public String update() {
    	userMapper.updateByPrimaryKeySelective(model);
        addActionMessage("User updated successfully");
        return "success";
    }
    
    public void setId(String id) {
        if (id != null) {
            this.model = userMapper.selectByPrimaryKey(Integer.valueOf(id));
        }
        this.id = id;
    }
    
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		if (this.getActionErrors().size() == 0) {
			if (!"admin".equals(user.getUiName())) {
				this.addActionError("you don't have auth to access");
			}
		}
	}

	@Override
	public Object getModel() {
		return (list != null ? list : model);
	}

	@Override
	public void setUserInfo(UserInfo user) {
		this.user = user;
	}

}
